package com.engineering.elink.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.engineering.elink.co.BaseCo;
import com.engineering.elink.co.BlogCo;
import com.engineering.elink.common.ResultType;
import com.engineering.elink.domain.Blog;
import com.engineering.elink.service.BlogService;
import com.engineering.elink.util.MapConvert;

/**
 * 博文接口
 * @author hxjxiaojun
 *@time 2016年11月30日
 */
@RestController
@RequestMapping("/api/blog")
@Api(value = "博文接口")
public class BlogController {
	@Autowired
	private BlogService   blogService;
	
	/**
	 * 获取博文列表
	 * @return
	 */
	@RequestMapping(value="/getList",method = RequestMethod.GET)
	@ApiOperation(value="获取博文列表",httpMethod="GET",notes="分页获取我的博文")
	public Map<String, Object>getList(@ModelAttribute @Valid  BlogCo co ,HttpServletRequest request){
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId = null;
		if (userInfo !=null) {
			userId =  userInfo.get("userId").toString();
			return blogService.getList(co,userId);
		}else{
			return MapConvert.getMap(ResultType.NEED_LOGIN);
		}
	}
	
	/**
	 * 获取他人的博文列表
	 * @param co
	 * @param userId
	 * @return
	 */
	@RequestMapping(value="/getBlogListByUserId",method = RequestMethod.GET)
	@ApiOperation(value="获取他人的博文列表",httpMethod="GET",notes="分页获取他人的博文")
	public Map<String, Object>getBlogListByUserId(@ModelAttribute @Valid  BaseCo co,
			@ApiParam(value="用户id",required=true)@RequestParam(value="userId",required=true)String userId,
			@ApiParam(value="类别0--个人博文1--企业博文 2--个人简历的站内作品,默认为0",required=true,defaultValue="0")
	@RequestParam(value="type",required=true,defaultValue="0")String type){
			return blogService.getBlogListByUserId(co,userId,type);	
	}	
	
	/**
	 * 获取博文详情
	 * @param blogId
	 * @param type
	 * @return
	 */
	@RequestMapping(value="/getInfo",method = RequestMethod.GET)
	@ApiOperation(value="获取博文详情",httpMethod="GET",notes="获取博文详情")
	public Map<String, Object>getInfo(@ApiParam(value="博文主键",required=true)@RequestParam(value="blogId",required=true)String blogId,
			@ApiParam(value="类别0--个人博文1--企业博文 2--个人简历的站内作品,默认为0",required=true,defaultValue="0")
	@RequestParam(value="type",required=true,defaultValue="0")int type ,HttpServletRequest request){
		
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId = null;
		if (userInfo !=null) {
			userId =  userInfo.get("userId").toString();
		}
		return blogService.getInfo(blogId,userId,type);
	}


	/**
	 * 添加站内作品
	 * @param blogIds
	 * @return
	 */
	@RequestMapping(value="/updateStationWorks",method = RequestMethod.GET)
	@ApiOperation(value="添加站内作品",httpMethod="GET",notes="将个人博文转换为个人站内作品")
	 public Map<String, Object>updateStationWorks(@ApiParam(value="博文主键，用逗号进行分割",required=true)
	 @RequestParam(value="blogIds",required=true)String blogIds,HttpServletRequest request){
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String	userId =  userInfo.get("userId").toString();
		 return blogService.updateStationWorks(blogIds,userId);
	 }
	
	/**
	 * 取消站内作品
	 * @param blogIds
	 * @return
	 */
	@RequestMapping(value="/delStationWorks",method = RequestMethod.GET)
	@ApiOperation(value="取消站内作品",httpMethod="GET",notes="将个人站内作品转换为个人博文")
	 public Map<String, Object>delStationWorks(@ApiParam(value="博文主键，用逗号进行分割",required=true)
	 @RequestParam(value="blogIds",required=true)String blogIds,HttpServletRequest request){
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String	userId =  userInfo.get("userId").toString();
		 return blogService.delStationWorks(blogIds,userId);
	 }
	
	/**
	 * 获取他人博文详情
	 * @param blogId
	 * @param userId
	 * @return
	 */
	@RequestMapping(value="/getInfoByUserId",method = RequestMethod.GET)
	@ApiOperation(value="获取他人博文详情",httpMethod="GET",notes="获取他人博文详情")
	public Map<String, Object>getInfoByUserId(
			@ApiParam(value="博文主键",required=true)@RequestParam(value="blogId",required=true)String blogId,
			@ApiParam(value="类别0--个人博文1--企业博文 2--个人简历的站内作品,默认为0",required=true,defaultValue="0")
	@RequestParam(value="type",required=true,defaultValue="0")int type,
	@ApiParam(value="他人的用户ID",required=true)@RequestParam(value="userId",required=true)String userId){
		
		return blogService.getInfoByUserId(blogId,userId,type);
	}
	
	/**
	 *  获取本人的其他博文
	 * @param co
	 * @param blogId
	 * @param type
	 * @return
	 */
	@RequestMapping(value="/getOtherBlog",method = RequestMethod.GET)
	@ApiOperation(value="获取本人的其他博文",httpMethod="GET",notes="分页获取本人的其他博文")
	public Map<String, Object>getOtherBlog(@ModelAttribute @Valid BaseCo co ,@RequestParam(value="blogId",required=true)String blogId,
			@ApiParam(value="类别0--个人博文1--企业博文 2--个人简历的站内作品,默认为0",required=true,defaultValue="0")
	@RequestParam(value="type",required=true,defaultValue="0")String type,HttpServletRequest request){
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId =  userInfo.get("userId").toString();
		return blogService.getOtherBlog(co,userId,blogId,type);
	}

	/**
	 * 分页获取他人的其他博文
	 * @param co
	 * @param blogId
	 * @param userId
	 * @param type
	 * @return
	 */
	@RequestMapping(value="/getOtherBlogByUserId",method = RequestMethod.GET)
	@ApiOperation(value="获取他人的其他博文",httpMethod="GET",notes="分页获取他人的其他博文")
	public Map<String, Object>getOtherBlogByUserId(@ModelAttribute @Valid BaseCo co ,@RequestParam(value="blogId",required=true)String blogId,
			@ApiParam(value="用户id",required=true)@RequestParam(value="userId",required=true)String userId,
			@ApiParam(value="类别0--个人博文1--企业博文 2--个人简历的站内作品,默认为0",required=true,defaultValue="0")
	@RequestParam(value="type",required=true,defaultValue="0")String type){

		return blogService.getOtherBlogByUserId(co,userId,blogId,type);
	}
	
	/**
	 * 添加博文
	 * @param blog
	 * @param dcIds
	 * @return
	 */
	@RequestMapping(value="/add",method = RequestMethod.POST)
	@ApiOperation(value="添加博文",httpMethod="POST",notes="添加博文")
	public Map<String, Object>add(@ModelAttribute @Valid Blog blog,@ApiParam(value="详情分类主键ids，用逗号进行分割",required=true)
	@RequestParam(value="dcIds",required=true)String dcIds,HttpServletRequest request){
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId =  userInfo.get("userId").toString();
		blog.setUserId(userId);
		return  blogService.add(blog,dcIds);
	}
	
	/**
	 * 修改博文
	 * @param blog
	 * @param dcIds
	 * @param oldDcIds
	 * @return
	 */
	@RequestMapping(value="/update",method = RequestMethod.POST)
	@ApiOperation(value="修改博文",httpMethod="POST",notes="修改博文")
	public Map<String, Object>update(@ModelAttribute @Valid Blog blog,
			@ApiParam(value="新的详情分类主键ids，用逗号进行分割",required=true)@RequestParam(value="dcIds",required=true)String dcIds,
			@ApiParam(value="旧的详情分类主键ids，用逗号进行分割",required=true)@RequestParam(value="oldDcIds",required=true)String oldDcIds
			){
		return  blogService.update(blog,dcIds,oldDcIds);
	}
	
	/**
	 * 删除博文
	 * @param blogId
	 * @return
	 */
	@RequestMapping(value="/del",method = RequestMethod.GET)
	@ApiOperation(value="删除博文",httpMethod="GET",notes="删除博文")
	public Map<String, Object>del(@ApiParam(value="博文主键",required=true)@RequestParam(value="blogid",required=true)String blogId){
		return  blogService.del(blogId);
	}	
	
	/**
	 * 人气博文
	 * @param coount
	 * @return
	 */
	@RequestMapping(value="/getGreetBlog",method = RequestMethod.GET)
	@ApiOperation(value="获取人气博文",httpMethod="GET",notes="获取人气博文")
	public Map<String, Object>getGreetBlog(@ApiParam(value="返回的数据的条数",required=false,defaultValue="10")
	@RequestParam(value="count",required=false,defaultValue="10")int count){
		return  blogService.getGreetBlog(count);
	}
	
	/**
	 * 获取浏览记录
	 * @param fkId
	 * @param type
	 * @return
	 */
	@RequestMapping(value="/getMark",method=RequestMethod.GET)
	@ApiOperation(value="获取浏览记录",httpMethod="GET")
	public Map<String, Object>getMark(
			@ApiParam(value="外键主键",required=true)@RequestParam(value="fkId",required=true)String fkId,
			@ApiParam(value="类型1--博文，2--企业，3--职位,默认1",required=true,defaultValue="1")
			@RequestParam(value="type",required=true,defaultValue="1")int type,
			@ApiParam(value="返回的数据的条数",required=false,defaultValue="10")@RequestParam(value="count",required=false,defaultValue="10")int count	
			,HttpServletRequest request){
		String userId =null;
		try {
			@SuppressWarnings("unchecked")
			Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
			userId =  userInfo.get("userId").toString();
		} catch (Exception e) {
		}
		return blogService.getMark(userId,fkId,type,count);
	}
}
